﻿#region Copyright ©2008-2011, Technische Universitaet Muenchen
// ====================================================================================================
//
// Last Changed by       $Author: then $
// Last Changed Date     $LastChangedDate: 2009-04-01 10:10:55 +0200 (Wed, 01 Apr 2009) $
// Last Changed Revision $Rev: 49 $
//
// ====================================================================================================
#endregion

using System.IO;
using System.Windows;
using System.Windows.Media;
using Microsoft.Win32;

using Tum.CollabXT.VModellXT.Properties;
using System;

namespace Tum.CollabXT.VModellXT
{
    /// <summary>
    /// Interaction logic for VModellConfigPage.xaml
    /// </summary>
    public partial class VModellConfigPage : VModellPage
    {
        VModellXTProcessProvider _ProcessProvider;
        IConversionWorkflow _ConversionWorkflow;


        public VModellConfigPage()
        {
            InitializeComponent();
        }

        internal override void ReceiveHandles(VModellConfigFrame configFrame, VModellXTProcessProvider processProvider, IConversionWorkflow conversionWorkflow, bool advance)
        {
            _ProcessProvider = processProvider as VModellXTProcessProvider;
            if (_ProcessProvider == null)
                throw new ArgumentNullException("processProvider");

            if (File.Exists(Settings.Default.LastVModellPath))
                _ProcessProvider.VModellFile = Settings.Default.LastVModellPath;

            _ConversionWorkflow = conversionWorkflow;

            SetValues();
        }

        private void buttonSetVModellFilePath_Click(object sender, RoutedEventArgs e)
        {
            var openFileDlg = new OpenFileDialog
                                  {
                                      Filter = VModellXT.Resources.Language.ConfigPage_VModellFiles + " (*.xml)|*.xml",
                                      Multiselect = false,
                                      RestoreDirectory = true
                                  };

            if (openFileDlg.ShowDialog() == true)
            {
                _ProcessProvider.VModellFile = openFileDlg.FileName;
                _ConversionWorkflow.InvalidateProcessing();
                SetValues();

                //Save path as 'last model file'
                Settings.Default.LastVModellPath = _ProcessProvider.VModellFile;
                Settings.Default.Save();
            }
        }

        private void buttonProjectFilePath_Click(object sender, RoutedEventArgs e)
        {
            var openFileDlg = new OpenFileDialog
                                  {
                                      Filter = VModellXT.Resources.Language.ConfigPage_VModellProjectFiles + " (*.vmp)|*.vmp",
                                      Multiselect = false,
                                      RestoreDirectory = true
                                  };

            if (openFileDlg.ShowDialog() == true)
            {
                _ProcessProvider.ProjectFile = openFileDlg.FileName;
                _ConversionWorkflow.InvalidateProcessing();
                SetValues();
            }
        }

        private void SetValues()
        {
            //V-Modell file
            if (_ProcessProvider.VModellFile == null)
                labelVModellFilePath.Content = VModellXT.Resources.Language.PathNotSet;
            else
            {
                labelVModellFilePath.Content = _ProcessProvider.VModellFile;
                labelVModellFilePath.Foreground = new SolidColorBrush(Colors.Black);
            }

            //Tailoring / Project file
            if (_ProcessProvider.UseInternalTailoring)
            {
                RadioButtonTailoring.IsChecked = true;

                labelProjectFilePath.Foreground = new SolidColorBrush(Colors.Gray);
            }
            else
            {
                RadioButtonProjectFile.IsChecked = true;

                if (_ProcessProvider.ProjectFile == null)
                {
                    labelProjectFilePath.Content = VModellXT.Resources.Language.PathNotSet;
                    labelProjectFilePath.Foreground = new SolidColorBrush(Colors.Red);
                }
                else
                {
                    labelProjectFilePath.Content = _ProcessProvider.ProjectFile;
                    labelProjectFilePath.Foreground = new SolidColorBrush(Colors.Black);
                }
            }

            //Button states
            UpdateWorkflowButtons();
        }

        private void UpdateWorkflowButtons()
        {
            if(_ProcessProvider != null)
            {
                _ConversionWorkflow.UpdateButtonState(true, _ProcessProvider.VModellFile != null && (_ProcessProvider.ProjectFile != null || RadioButtonTailoring.IsChecked.Value));
            }
        }

        private void RadioButtonProjectFile_Checked(object sender, RoutedEventArgs e)
        {
            if(!IsInitialized)
                return;
            labelProjectFile.Foreground = new SolidColorBrush(Colors.Black);
            buttonProjectFilePath.IsEnabled = true;

            _ProcessProvider.UseInternalTailoring = false;

            SetValues();
        }

        private void RadioButtonTailoring_Checked(object sender, RoutedEventArgs e)
        {
            if (!IsInitialized)
                return;
            labelProjectFile.Foreground = new SolidColorBrush(Colors.Gray);
            buttonProjectFilePath.IsEnabled = false;

            _ProcessProvider.UseInternalTailoring = true;

            SetValues();
        }

        internal override bool ChangePage(bool advance)
        {
            if (!advance)
                return true;

            if (_ProcessProvider.VModellFile != null && (_ProcessProvider.ProjectFile != null || RadioButtonTailoring.IsChecked.Value))
                return true;
            return false;
        }
    }
}
